package com.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.entity.*;
import com.service.MenuItemService;
import com.service.RolePermissionService;
import com.service.UserPermissionService;
import com.util.ButtonItemPojo;
import com.util.MenuItemPojo;
import com.util.ReturnResult;
import com.util.TreePojo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/api")
public class MenuItemController {
    
    @Resource
    private MenuItemService service;
    
    @Resource
    private RolePermissionService rolePermissionService;
    
    @Resource
    private UserPermissionService userPermissionService;
    
    @RequestMapping("/menuitem/select")
    @ResponseBody
    public ReturnResult selectMenu(HttpSession session){
        ReturnResult result = new ReturnResult();
        User user = (User)session.getAttribute("user");
        
        if(user == null){
            result.returnFail("您未登录,请登录！");
            return result;
        }else {
            int id = user.getId();
            if(userPermissionService.selectCount(new EntityWrapper<UserPermission>().eq("userId",user.getId())) > 0){
                
                List<MenuItem> menuItemList = service.selectFatherMenuByUser(id);
                List<MenuItemPojo> list = new ArrayList<>();
                for (MenuItem menuItem:menuItemList) {
                    MenuItemPojo itemPojo = new MenuItemPojo();
                    itemPojo.setId(menuItem.getId());
                    itemPojo.setName(menuItem.getName());
                    itemPojo.setPath(menuItem.getPath());
                    itemPojo.setIcon(menuItem.getIcon());
                    List<MenuItem> childMenuItemList = service.selectChildMenuByUser(menuItem.getId(),id);
                    List<MenuItemPojo> childList = new ArrayList<>();
                    for(MenuItem item : childMenuItemList){
                        MenuItemPojo itemPojo_child = new MenuItemPojo();
                        itemPojo_child.setId(item.getId());
                        itemPojo_child.setName(item.getName());
                        itemPojo_child.setPath(item.getPath());
                        itemPojo_child.setIcon(item.getIcon());
                        itemPojo_child.setChildren(null);
                        childList.add(itemPojo_child);
                    };
                    itemPojo.setChildren(childList);
                    list.add(itemPojo);
                }
                result.returnSuccess(list,"查询列表成功！");
            }else {
                List<MenuItem> menuItemList = service.selectFatherMenu(id);
                List<MenuItemPojo> list = new ArrayList<>();
                for (MenuItem menuItem:menuItemList) {
                    MenuItemPojo itemPojo = new MenuItemPojo();
                    itemPojo.setId(menuItem.getId());
                    itemPojo.setName(menuItem.getName());
                    itemPojo.setPath(menuItem.getPath());
                    itemPojo.setIcon(menuItem.getIcon());
                    List<MenuItem> childMenuItemList = service.selectChildMenu(menuItem.getId(),id);
                    List<MenuItemPojo> childList = new ArrayList<>();
                    for(MenuItem item : childMenuItemList){
                        MenuItemPojo itemPojo_child = new MenuItemPojo();
                        itemPojo_child.setId(item.getId());
                        itemPojo_child.setName(item.getName());
                        itemPojo_child.setPath(item.getPath());
                        itemPojo_child.setIcon(item.getIcon());
                        itemPojo_child.setChildren(null);
                        childList.add(itemPojo_child);
                    };
                    itemPojo.setChildren(childList);
                    list.add(itemPojo);
                }
                result.returnSuccess(list,"查询列表成功！");
            }
        }
        return result;
    }
    
    @RequestMapping("/button/selectBtn/{pid}")
    @ResponseBody
    public ReturnResult selectBtn(@PathVariable("pid") int pid, HttpSession session){
        ReturnResult result = new ReturnResult();
        User user = (User) session.getAttribute("user");
        if(user == null){
            result.returnFail("您未登录");
            return result;
        }else {
            int id = user.getId();
            if(userPermissionService.selectCount(new EntityWrapper<UserPermission>().eq("userId",id)) > 0){
                List<MenuItem> menuItemList = service.selectBtnByUser(pid,id);
                List<ButtonItemPojo> list = new ArrayList<>();
                for(MenuItem item : menuItemList){
                    list.add(new ButtonItemPojo(item.getId(),item.getName(),item.getButton()));
                }
                result.returnSuccess(list,"查询按钮成功！");
            }else {
                List<MenuItem> menuItemList = service.selectBtn(pid,id);
                List<ButtonItemPojo> list = new ArrayList<>();
                for(MenuItem item : menuItemList){
                    list.add(new ButtonItemPojo(item.getId(),item.getName(),item.getButton()));
                }
                result.returnSuccess(list,"查询按钮成功！");
            }
        }
        return result;
    }
    
    @RequestMapping("/menuItem/selectList")
    @ResponseBody
    public ReturnResult selectList(){
        ReturnResult result = new ReturnResult();
        List<MenuItem> itemList = service.selectList(new EntityWrapper<MenuItem>().eq("pid",0));
        List<TreePojo> list = new ArrayList<>();
        for (MenuItem item:itemList){
            TreePojo treePojo = new TreePojo();
            treePojo.setId(item.getId());
            treePojo.setLabel(item.getName());
            List<TreePojo> children = new ArrayList<>();
            List<MenuItem> childrenList = service.selectList(new EntityWrapper<MenuItem>().eq("pid",item.getId()));
            for(MenuItem childItem : childrenList){
                TreePojo treePojo1 = new TreePojo();
                treePojo1.setId(childItem.getId());
                treePojo1.setLabel(childItem.getName());
                List<TreePojo> btnChildren = new ArrayList<>();
                List<MenuItem> btnChildrenList = service.selectList(new EntityWrapper<MenuItem>().eq("pid",childItem.getId()));
                for(MenuItem btnItem : btnChildrenList){
                    TreePojo treePojo2 = new TreePojo();
                    treePojo2.setId(btnItem.getId());
                    treePojo2.setLabel(btnItem.getName());
                    treePojo2.setChildren(null);
                    btnChildren.add(treePojo2);
                }
                treePojo1.setChildren(btnChildren);
                children.add(treePojo1);
            }
            treePojo.setChildren(children);
            list.add(treePojo);
        }
        result.returnSuccess(list,"查询成功！");
        return result;
    }
    
    @RequestMapping("/menuItem/selectList/{page}/{limit}")
    @ResponseBody
    public ReturnResult selectList(@PathVariable("page") int page,@PathVariable("limit") int limit){
        ReturnResult result = new ReturnResult();
        Page<MenuItem> pageConfig = new Page<>(page,limit);
        pageConfig.setTotal(service.selectCount(null));
        Page<MenuItem> rolePage = service.selectPage(pageConfig,null);
        result.returnSuccess(rolePage,"查询成功！");
        return result;
    }
    
    @RequestMapping("/menuItem/selectOne/{pid}")
    @ResponseBody
    public ReturnResult selectOne(@PathVariable("pid") int pid){
        ReturnResult result = new ReturnResult();
        MenuItem item = service.selectById(pid);
        result.returnSuccess(item,"查询成功！");
        return result;
    }
    
    @RequestMapping("/menuItem/selectAll")
    @ResponseBody
    public ReturnResult selectAll(){
        ReturnResult result = new ReturnResult();
        List<MenuItem> items = service.selectList(null);
        result.returnSuccess(items,"查询成功！");
        return  result;
    }
    
    @RequestMapping("/menuItem/add")
    @ResponseBody
    public ReturnResult add(@RequestBody MenuItem item){
        ReturnResult result = new ReturnResult();
        boolean result_Add = service.insert(item);
        if(result_Add)
            result.returnSuccess(result_Add,"新增成功！");
        else 
            result.returnFail("新增失败！");
        return result;
    }
    
    @RequestMapping("/menuItem/upd")
    @ResponseBody
    public ReturnResult upd(@RequestBody MenuItem item){
        ReturnResult result = new ReturnResult();
        boolean result_Upd = service.updateById(item);
        if(result_Upd)
            result.returnSuccess(result_Upd,"修改成功！");
        else
            result.returnFail("修改失败！");
        return result;
    }
    
    @RequestMapping("/menuItem/del/{id}")
    @ResponseBody
    public ReturnResult del(@PathVariable("id") int id){
        ReturnResult result = new ReturnResult();
        boolean result_DelRolePermission = rolePermissionService.delete(new EntityWrapper<RolePermission>().eq("menuId",id));
        boolean result_DelUserPermission = userPermissionService.delete(new EntityWrapper<UserPermission>().eq("menuId",id));
        if(result_DelRolePermission == result_DelUserPermission){
            boolean result_Del = service.deleteById(id);
            if(result_Del)
                result.returnSuccess(result_Del,"删除成功！");
            else 
                result.returnFail("删除失败！");
        }else{
            if(result_DelRolePermission)
                result.returnFail("删除角色权限成功，删除用户权限失败！");
            if(result_DelUserPermission)
                result.returnFail("删除用户权限成功，删除角色权限失败！");
        }
        return result;
    }
}
